﻿@model Xms.Web.Customize.Models.EditReportModel

<link href="@app.PlatformSettings.CSSCDN/content/css/sumoselect.css?v=@app.PlatformSettings.VersionNumber" rel="stylesheet" />
<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">
            <a data-toggle="collapse"
               href="#collapseTwo">
                <strong>@app.PrivilegeTree?.LastOrDefault().DisplayName</strong>
            </a>
        </h3>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse in">
        <div class="panel-body">
            <div class="form-group col-sm-12">
                <div class="col-sm-10">
                </div>
            </div>
            <form action="/@app.OrganizationUniqueName/customize/@app.ControllerName/@app.ActionName" method="post" enctype="multipart/form-data" data-autoreset="true" id="editform" class="form-horizontal" role="form">
                @Html.ValidationSummary()
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.Name, "名称", new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @Html.TextBoxFor(x => x.Name, new { @class = "form-control required" })
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    @Html.LabelFor(x => x.Description, "描述", new { @class = "col-sm-2 control-label" })
                    <div class="col-sm-10">
                        @Html.TextAreaFor(x => x.Description, new { @class = "form-control" })
                    </div>
                </div>
                @Html.HiddenFor(x => x.CustomConfig)
                @Html.HiddenFor(x => x.QueryConfig)
                @Html.HiddenFor(x => x.TypeCode)
                @Html.HiddenFor(x => x.DefaultFilterConfig)
                @Html.HiddenFor(x => x.EntityId)
                @Html.HiddenFor(x => x.RelatedEntityId)
                <div class="form-group col-sm-12">
                    <label class="col-sm-2 control-label">@app.T["report_source"]</label>
                    <div class="col-sm-10">
                        <select id="report-type" class="form-control">
                            <option value="pilot">@app.T["report_wizard"]</option>
                            <option value="file">@app.T["report_file"]</option>
                            <option value="url">@app.T["report_webpage_link"]</option>
                        </select>
                    </div>
                </div>
                <div id="report-file" style="display:none;" class="form-group col-sm-12">
                    <label class="col-sm-2 control-label">@app.T["file"]</label>
                    <div class="col-sm-10">
                        <input type="file" name="reportfile" class="form-control" />
                    </div>
                </div>
                <div id="report-pilot" class="form-group col-sm-12">
                    <label class="col-sm-2 control-label">@app.T["report_wizard"]</label>
                    <div class="col-sm-10">
                        <button type="button" onclick="OpenReportPilot()" class="btn btn-default">@app.T["report_wizard"]</button>
                    </div>
                </div>
                <div id="report-url" style="display:none;" class="form-group col-sm-12">
                    <label class="col-sm-2 control-label">@app.T["report_webpage"]</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" value="" />
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    <label class="col-sm-2 control-label">@app.T["report_owner_entity"]</label>
                    <div class="col-sm-10">
                        <select id="EntityList" class="form-control"></select>
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    <label class="col-sm-2 control-label">@app.T["report_associative_entity"]</label>
                    <div class="col-sm-10">
                        <select id="RelevantEntityList" class="form-control">
                            <option value="">@app.T["please_select"]</option>
                        </select>
                    </div>
                </div>
                <div class="form-group col-sm-12">
                    <label class="col-sm-2 control-label">@app.T["parent_report"]</label>
                    <div class="col-sm-10">
                        <select class="form-control"></select>
                    </div>
                </div>
                <div class="form-group col-sm-12 text-center" id="form-buttons">
                    <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-saved"></span> @app.T["save"]</button>
                    <button type="reset" class="btn btn-default"><span class="glyphicon glyphicon-refresh"></span> @app.T["reset"]</button>
                </div>
            </form>
        </div>
    </div>
</div>
@section Scripts {
    <script src="/content/js/jquery.form.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-validate/jquery.validate.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-validate/localization/messages_zh.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery.sumoselect.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/xms.metadata.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script>
        var isEditReport = false;
        $(function () {
            //表单验证
            Xms.Web.Form($("#editform"), null, null, function () {
                var selectVal = $('#report-type').find('option:selected').val();
                Xms.Web.Event.localStorageEvent.trigger('list_report_rebind');

                if ($('#report-file').find('input[type=file]').val() == '' && selectVal == 'file') {
                    Xms.Web.Alert(false, '请选择文件', null, null);
                    return false;
                }
                else if (($('#CustomConfig').val() == '' || $('#QueryConfig').val() == '' || $('#DefaultFilterConfig').val() == '') && selectVal == 'pilot') {
                    Xms.Web.Alert(false, '请完善报表向导内容', null, null);
                    return false;

                } else if ($('#report-url').find('input[type=text]').val() == '' && selectVal == 'url') {
                    Xms.Web.Alert(false, '请输入网址', null, null);
                    return false;
                }
                var $isauthorization = $('#IsAuthorization');
                if ($isauthorization.prop('checked')) {
                    if (!$('#securityUser').val() || $('#securityUser').val() == '') {
                        Xms.Web.Alert(false, "请先勾选角色");
                        return false;
                    }
                }
            });
            $('body').on('change', '#EntityList', function () {
                var entityid = $(this).find('option:selected').val();
                $('#EntityId').val(entityid);
                loadRelevantEntities(entityid);
                RemovePilot();
            });
            $('body').on('change', '#RelevantEntityList', function () {
                var relatedentityid = $(this).find('option:selected').val();
                $('#RelatedEntityId').val(relatedentityid);
                RemovePilot();
            });
            $('body').on('change', '#report-type', function () {
                var type = $(this).find('option:selected').val();
                if (type == 'pilot') {
                    $('#report-pilot').show();
                    $('#report-file').hide();
                    $('#report-url').hide();
                    $('#TypeCode').val(0);
                }
                else if (type == 'file') {
                    $('#report-pilot').hide();
                    $('#report-file').show();
                    $('#report-url').hide();
                    $('#TypeCode').val(1);
                }
                else if (type == 'url') {
                    $('#report-pilot').hide();
                    $('#report-file').hide();
                    $('#report-url').show();
                    $('#TypeCode').val(2);
                }
            });

            loadEntities();
        });

        function renderModityByData(data, type) {
            var _html = [];
            $.each(data, function (i, n) {
                var flag = false;
                n['rolesid'] = n['roleid'];
                _html.push('<option class="list-group-item" data-entityname="type" value="' + n['rolesid'] + '" data-value="' + n['rolesid'] + '">');
                _html.push(n.name);
                _html.push('</option>');
            });
            return _html.join('');
        }
        function loadEntities() {
            Xms.Schema.GetEntities({ 'GetAll': true }, function (data) {
                if (!data || data.length == 0) return;
                $(data).each(function (i, n) {
                    $('#EntityList').append('<option data-relationship="' + n.name + '"  value="' + n.entityid + '">' + n.localizedname + '</option>');
                });
                loadRelevantEntities($('#EntityList').find('option:selected').val());
            });
        }

        //加载实体
        function loadRelevantEntities(entityid) {
            Xms.Schema.GetEntityRelations(null, entityid, function (data) {
                if (!data || data.length == 0) return;
                $('#RelevantEntityList').html('');
                $('#RelevantEntityList').append('<option value="">--请选择--</option>');
                $(data).each(function (i, n) {
                    $('#RelevantEntityList').append('<option data-referencingentitylocalizedname="' + n.referencingentitylocalizedname + '" data-referencingentityname="' + n.referencingentityname + '" data-referencedattributename="' + n.referencedattributename + '" data-referencingattributename="' + n.referencingattributename + '" data-referencedentityname="' + n.referencedentityname + '" data-relationship="' + n.name + '" data-referencingattributelocalizedname="' + n.referencingattributelocalizedname + '" value="' + n.referencingentityid + '">' + n.referencingentitylocalizedname + '(' + n.referencedentitylocalizedname + ')' + '</option>');
                });
            });
        }
        function OpenReportPilot() {
            if ($('#PilotModal').length == 0) {
                Xms.Web.OpenDialog('/customize/report/reportpilot');
            }
            else {
                $('#PilotModal').modal('show');
            }
        }
        function RemovePilot() {
            if ($('#PilotModal').length > 0) {
                $('#PilotModal').remove();
            }
        }
    </script>
}